package com.huy.study;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.builder.Controller;
import com.baomidou.mybatisplus.generator.config.builder.Entity;
import com.baomidou.mybatisplus.generator.config.builder.Mapper;
import com.baomidou.mybatisplus.generator.config.builder.Service;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import org.junit.jupiter.api.Test;

import java.sql.Types;
import java.util.Collections;

public class MybatisPlusGenerator {


    private static String  url="jdbc:mariadb://192.168.1.107:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true";
    private static String username="root";
    private static String password="root";
    private static String schema="test";

    @Test
    public void test1(){
        DataSourceConfig.Builder dataSourceConfigBuilder = builderDataSourceConfig();
        FastAutoGenerator.create(dataSourceConfigBuilder)
                .globalConfig(builder -> buildGlobalConfig(builder))
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    return typeRegistry.getColumnType(metaInfo);

                }))
                .packageConfig(builder ->buildPackageConfig(builder))
                .strategyConfig(builder ->buildStrategyConfig(builder))
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }


    private DataSourceConfig.Builder builderDataSourceConfig(){
        return new DataSourceConfig
                .Builder(url, username, password)
                .schema(schema);
    }

    private GlobalConfig buildGlobalConfig(GlobalConfig.Builder builder){
          String path=System.getProperty("user.dir");
       return  builder
                .author("huy")
                .disableOpenDir()
                .outputDir(String.format("%s/%s",path,"src/main/java"))
                .dateType(DateType.TIME_PACK)
                .build();
    }
    private PackageConfig buildPackageConfig(PackageConfig.Builder builder){
        return  builder.parent("com.huy.study")
                .entity("entity")
                .service("service")
                .serviceImpl("service.impl")
                .mapper("mapper")
                .xml("mapper.xml")
                .controller("controller")
                .pathInfo(Collections.singletonMap(OutputFile.xml, "src/main/resources/mapper"))
                .build();
    }

    private StrategyConfig buildStrategyConfig(StrategyConfig.Builder builder){
        StrategyConfig.Builder strategyConfigBuilder = builder
                .addTablePrefix("t_")
                .addFieldPrefix("c_","dt_");
        Entity.Builder entityBuilder=entityBuilder(strategyConfigBuilder);
        Controller.Builder  controllerBuilder=controllerBuilder(entityBuilder);
        Service.Builder  serviceBuilder=serviceBuilder(controllerBuilder);
        Mapper.Builder  mapperBuilder=mapperBuilder(serviceBuilder);
        return  mapperBuilder.build();
    }
    private  Entity.Builder entityBuilder(StrategyConfig.Builder builder){
        Column cjsj = new Column("dt_cjsj", FieldFill.INSERT);
        Column zhxgsj = new Column("dt_zhxgsj", FieldFill.INSERT_UPDATE);
        return builder
                .entityBuilder()
                .enableFileOverride()
                .enableTableFieldAnnotation()
                .idType(IdType.ASSIGN_UUID)
                .disableSerialVersionUID()
                .addTableFills(cjsj,zhxgsj)
                .formatFileName("%sEntity");
    }
    private  Controller.Builder controllerBuilder(Entity.Builder entityBuilder){
       return entityBuilder
                .controllerBuilder()
                .enableFileOverride()
                .enableRestStyle()
               .formatFileName("%scontroller")	;
    }
    private  Service.Builder serviceBuilder(Controller.Builder  controllerBuilder){
        return  controllerBuilder
                .serviceBuilder()
                .enableFileOverride();

    }
    private  Mapper.Builder mapperBuilder(Service.Builder serviceBuilder){
        return   serviceBuilder
                .mapperBuilder()
                .enableFileOverride()
                .enableBaseResultMap()
                .enableBaseColumnList();
    }


}
